package com.qipay.sms.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
import com.google.gson.Gson;
import com.qipay.sms.config.AliyunSMSCodeEnum;
import com.qipay.sms.config.AliyunSMSTemplateEnum;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Creator: Yao
 * Date:    2017/10/20
 * For:
 * Other:
 */
@Component
public class ALiyunSMSOldUtils {

    final static Logger logger = LogManager.getLogger(ALliyunSMSUtils.class);
    final static Gson gson = new Gson();

    @Value("${aliyun.sms.product:}")
    private String product;
    @Value("${aliyun.sms.domain:}")
    private String domain;
    @Value("${aliyun.sms.timeout.connect:}")
    private String defaultConnectTimeout;
    @Value("${aliyun.sms.timeout.read:}")
    private String defaultReadTimeout;

    @Value("${aliyun.sms.access.key.id:}")
    private String accessKeyId;
    @Value("${aliyun.sms.access.key.secret:}")
    private String accessKeySecret;
    @Value("${aliyun.sms.sign:}")
    private String sign;

    /***
     * 单条短信发送 - 推荐
     * @param phone     手机号
     * @param template  短信模板
     * @param info      模板所需填充参数值
     * @return
     */
    public String send(String phone, String template, String info) {

        // check
        Assert.isTrue(StringUtils.isNotBlank(phone));
        Assert.isTrue(StringUtils.isNotBlank(template));
        Assert.isTrue(StringUtils.isNotBlank(info));

//        product = "Sms";
//        domain = "sms.aliyuncs.com";
//        defaultConnectTimeout = "10000";
//        defaultReadTimeout = "10000";
//        accessKeyId = "LTAIgOwnqcOwLMX2";
//        accessKeySecret = "KERPoerdIeNtpvbj1LrFlI5WX7AxlK";
//        sign = "琦付天下";

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            return print(e);
        }
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendSmsRequest request = new SingleSendSmsRequest();
        try {
            request.setSignName(sign);
            request.setTemplateCode(template);
            request.setParamString(info);
            request.setRecNum(phone);
            SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
            logger.info(gson.toJson(httpResponse));
        } catch (ServerException e) {
            return print(e);
        } catch (ClientException e) {
            return print(e);
        } catch (Exception e) {
            return print(e);
        }

        return AliyunSMSCodeEnum.OK.getInfo();
    }

    private String print(Exception e) {

        // check
        Assert.notNull(e);

        // init
        String msg;

        // msg.error
        if (e instanceof ClientException) {
            msg = ((ClientException) e).getErrMsg();
        } else {
            msg = e.getMessage();
        }

        // log
        logger.error(String.format("sms.send.error= %s", msg));
        e.printStackTrace();
        return msg;
    }

    public static void main(String[] args) {
        ALiyunSMSOldUtils aLiyunSMSOldUtils = new ALiyunSMSOldUtils();
        aLiyunSMSOldUtils.send("11111111111", AliyunSMSTemplateEnum.BUSINESS.getCode(), String.format(AliyunSMSTemplateEnum.BUSINESS.getTemplate(), "xxx"));
    }
}
